From 2badab8c8391a0f12a1783ee2a785277d1ce1202 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 22 Dec 2014 13:58:24 -0800 Subject: [PATCH] Allow the location of `~/.cargo` to be overridden via `CARGO_HOME` env var. I intend to use this to allow many copies of the Rust toolchain to coexist. --- src/cargo/util/config.rs | 10 ++++++++-- tests/test_cargo.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index a796bb380..508f6edf1 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -36,7 +36,7 @@ impl<'a> Config<'a> { let (rustc_version, rustc_host) = try!(ops::rustc_version()); Ok(Config { - home_path: try!(os::homedir().require(|| { + home_path: try!(homedir().require(|| { human("Cargo couldn't find your home directory. \ This probably means that $HOME was not set.") })), @@ -248,6 +248,12 @@ impl ConfigValue { } } +fn homedir() -> Option { + let cargo_home = os::getenv("CARGO_HOME").map(|p| Path::new(p)); + let user_home = os::homedir(); + return cargo_home.or(user_home); +} + pub fn get_config(pwd: Path, key: &str) -> CargoResult { find_in_tree(&pwd, |file| extract_config(file, key)).map_err(|_| human(format!("`{}` not found in your configuration", key))) @@ -313,7 +319,7 @@ fn walk_tree(pwd: &Path, // Once we're done, also be sure to walk the home directory even if it's not // in our history to be sure we pick up that standard location for // information. - let home = try!(os::homedir().require(|| { + let home = try!(homedir().require(|| { human("Cargo couldn't find your home directory. \ This probably means that $HOME was not set.") })); diff --git a/tests/test_cargo.rs b/tests/test_cargo.rs index 773754d69..8ae8aa2c0 100644 --- a/tests/test_cargo.rs +++ b/tests/test_cargo.rs @@ -1,5 +1,6 @@ use std::io::fs::{mod, PathExtensions}; use std::io; +use std::io::{USER_RWX, File}; use std::os; use std::str; use cargo::util::process; @@ -73,3 +74,28 @@ test!(find_closest_dont_correct_nonsense { .with_stderr("No such subcommand ")); }); + +test!(override_cargo_home { + let root = paths::root(); + let my_home = root.join("my_home"); + fs::mkdir(&my_home, USER_RWX).assert(); + fs::mkdir(&my_home.join(".cargo"), USER_RWX).assert(); + File::create(&my_home.join(".cargo/config")).write_str(r#" + [cargo-new] + name = "foo" + email = "bar" + git = false + "#).assert(); + + assert_that(process(cargo_dir().join("cargo")).unwrap() + .arg("new").arg("foo") + .cwd(paths::root()) + .env("USER", Some("foo")) + .env("HOME", Some(paths::home())) + .env("CARGO_HOME", Some(my_home.clone())), + execs().with_status(0)); + + let toml = paths::root().join("foo/Cargo.toml"); + let toml = File::open(&toml).read_to_string().assert(); + assert!(toml.as_slice().contains(r#"authors = ["foo "]"#)); +}); -- 2.30.2